<?php

require_once 'include/language_manager.inc.php';
Language_Manager::getInstance();

$meta["title"] = L::meta_articles_title;
$meta["description"] = L::meta_articles_description;
$meta["keywords"] = L::meta_articles_keywords;

require "include/template_manager.inc.php";
require "include/image_manager.inc.php";

$myTemplate = new Template_Manager("lista_articoli", null, true, 8, $meta);
$query = "SELECT COUNT(*) AS conta FROM notizie";
$num = 6;
$range = 2;
if ($data = DB_Manager::getInstance()->query($query)) {
    $row = $data->fetch_array(MYSQLI_ASSOC);
    $last = (int) ($row['conta'] / $num);
}

$prefix = "/";
$labelNews = "articoli";
$labelArticle = "articolo";
if (getLanguage() == 'en') {
    $prefix = "/en/";
    $labelNews = "news";
    $labelArticle = "article";
}

if (!isset($_REQUEST['pagina']) || $_REQUEST['pagina'] == 1) {
    $myTemplate->setCanonicalUrl($prefix . $labelNews . "/");
    $pagina = 0;
} else {
    $myTemplate->setCanonicalUrl($prefix . $labelNews . "/" . $_REQUEST['pagina'] . "/");
    $pagina = (int) $_REQUEST['pagina'] - 1;
}
$start = $num * $pagina;
$query = "SELECT `n`.`Id` AS `id`, `n`.`Titolo` AS `titolo`, `n`.`Titolo_en` AS `titolo_en`,`n`.`Data` as `data`,"
        . "`i`.`uri` as `uri`, concat(`p`.`nome`,' ',`p`.`cognome`) AS `autore`,"
        . "`i`.`descrizione` AS `descrizione`, `i`.`descrizione_en` AS `descrizione_en`"
        . "FROM ((`notizie` `n` join `immagine` `i`) join `professori` `p`) "
        . "WHERE (`n`.`immagine` = `i`.`id`) and (`n`.`autore` = `p`.`id`) "
        . "ORDER BY `n`.`data` DESC LIMIT " . $start . ", " . $num;
if ($data = DB_Manager::getInstance()->query($query)) {
    while ($row = $data->fetch_array(MYSQLI_ASSOC)) {
        $myTemplate->setBodyContentString("id", $row['id']);
        $titolominlen = 50;
        if (getLanguage() == 'it') {
            $titolo = $row['titolo'];
            $descrizione = $row['descrizione'];
        } else {
            $titolo = $row['titolo_en'];
            $descrizione = $row['descrizione_en'];
        }
        
        $titolominlen-=strlen($titolo);
        $completa_titolo = '';
        if ($titolominlen > 0) {
            while ($titolominlen--) {
                $completa_titolo.=" &nbsp;";
            }
        }
        "Dall’Austria esaltano la cucina emiliana";
        $myTemplate->setBodyContentString("completa_titolo", $completa_titolo);
        $myTemplate->setBodyContentString("titolo", $titolo);
        $myTemplate->setBodyContentString("link_titolo", Config::$site_name . $prefix . $labelArticle . "/" . $titolo . '/');
        $myTemplate->setBodyContentString("autore", $row['autore']);
        $myTemplate->setBodyContentImage("link_immagine", $row['uri']);
        $myTemplate->setBodyContentImage("link_immagine_thumb", Image_Manager::thumb($row['uri']));
        $myTemplate->setBodyContentString("datapubb", getData($row['data']));
        $introlen = 180;
        $descr = (strlen($descrizione) > $introlen) ? substr($descrizione, 0, strpos($descrizione, ' ', $introlen)) . "..." : $descrizione;
        $myTemplate->setBodyContentString("descr", $descr);
    }
}
$s = ($pagina > $range) ? $pagina - $range : 0;
$list = "";
if ($pagina > 0) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelNews . "/" . $pagina . '/">' . L::messages_articles_list_prev . '</a></li>';
}
for (; $s < $pagina; $s++) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelNews . "/" . ($s + 1) . '/">' . ($s + 1) . '</a></li>';
}
$list = $list . '<li><a href="#" class="current">' . ($s + 1) . '</a></li>';
$s++;
$end = (($pagina + $range) > $last) ? $last + 1 : ($pagina + $range) + 1;
for (; $s < $end; $s++) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelNews . "/" . ($s + 1) . '/">' . ($s + 1) . '</a></li>';
}
if ($pagina < $last) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelNews . "/" . ($pagina + 2) . '/">' . L::messages_articles_list_next . '</a></li>';
}
$myTemplate->setBodyContentString("lista", $list);


$myTemplate->setBodyContentString("nav_message", L::nav_message);
$myTemplate->setBodyContentString("nav_home", L::nav_home);
$myTemplate->setBodyContentString("nav_page", L::messages_articles_nav_page);

$myTemplate->setBodyContentString("read_all", L::messages_articles_read_all);
